package com.beerandjesus.sportofkings.handicapper.strategy.brisnet.singlefile;

import com.beerandjesus.sportofkings.handicapper.model.MaidenHorseAnalysisResultImpl;
import com.beerandjesus.sportofkings.handicapper.model.ResultType;
import com.beerandjesus.sportofkings.handicapper.model.brisnet.singlefile.Horse;
import com.beerandjesus.sportofkings.handicapper.model.brisnet.singlefile.PastPerformance;
import com.beerandjesus.sportofkings.handicapper.model.brisnet.singlefile.Race;

public class MaidenSpeedResolver extends AbstractResolver {
    private static final int SPEED_ADJUSTMENT_FOR_1ST_OUT_64_PLUS = 15;
    private static final int SPEED_ADJUSTMENT_FOR_1ST_OUT_UNDER_64 = 30;
    private static final int THRESHOLD_WHERE_IMPROVEMENT_IS_BIGGER = 64;

    public MaidenSpeedResolver() {
    }

    void resolveSpeedFigures(MaidenHorseAnalysisResultImpl result, Horse h, Race r) {
        // Speed par
        Integer speedPar = getInteger(r.getBrisSpeedPar(), -1);
        ResultType resultType = result.getResultType();

        SpeedFigureResults speedFigureResults = new SpeedFigureResults();
        if (resultType.equals(ResultType.SECOND_TIME_STARTER)) {
            speedFigureResults = resolveSpeedFigureForSecondTimeStarters(h, speedPar);
        } else if (resultType.equals(ResultType.OLDER_MAIDEN)) {
            speedFigureResults = resolveSpeedForOlderMaidens(h, speedPar);
        }
        result.setParSpeedCount(speedFigureResults.speedParCount);
        result.setSpeedFigure(speedFigureResults.notation + speedFigureResults.maxSpeed);

    }

    private SpeedFigureResults resolveSpeedFigureForSecondTimeStarters(Horse h, Integer speedPar) {
        SpeedFigureResults speedFigureResults = new SpeedFigureResults();
        // 2nd starters:
        // Speed figure improvement 9-15pts for 65+
        // Poss improvement up to 30 for <64
        PastPerformance perf = h.getPastPerformances().get(0);
        String spd = perf.getBrisSpeedRating();
        try {
            Integer s = new Integer(spd);
            if (s >= THRESHOLD_WHERE_IMPROVEMENT_IS_BIGGER) {
                s += SPEED_ADJUSTMENT_FOR_1ST_OUT_64_PLUS;
                speedFigureResults.notation = "*";
            } else {
                s += SPEED_ADJUSTMENT_FOR_1ST_OUT_UNDER_64;
                speedFigureResults.notation = "**";
            }
            if (s >= speedPar) {
                speedFigureResults.speedParCount = 1;
            }
            speedFigureResults.maxSpeed = s;
        } catch (NumberFormatException e) {
            // Ignore
        }
        return speedFigureResults;
    }

    private SpeedFigureResults resolveSpeedForOlderMaidens(Horse h, Integer speedPar) {
        // Speed analysis
        SpeedFigureResults speedFigureResults = new SpeedFigureResults();
        int count = 0;
        for (PastPerformance perf : h.getPastPerformances()) {
            Integer finishPos = getInteger(perf.getFinishPosition(), 99);
            count++;
            speedFigureResults.lossCount++;
            if (finishPos == 1) {
                speedFigureResults.lossCount = 0;
            }
            Integer speed = getInteger(perf.getBrisSpeedRating(), 0);
            if (speed >= speedPar) {
                speedFigureResults.speedParCount++;
            }
            if (speed > speedFigureResults.maxSpeed) {
                speedFigureResults.maxSpeedAge = count;
                speedFigureResults.maxSpeed = speed;
            }
        }
        // Discount horses with high speed a few back followed by more recent losses
        if (speedFigureResults.maxSpeedAge > 2 && speedFigureResults.lossCount >= 2) {
            speedFigureResults.notation = "-";
        }
        return speedFigureResults;
    }

    private final class SpeedFigureResults {
        protected int speedParCount = 0;
        protected int maxSpeed = 0;
        protected int maxSpeedAge = 0;
        protected int lossCount = 0;
        protected String notation = "";
    }

}